﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections;
using MidwayBattle.Engine;
using MidwayBattle.Models;
using MidwayBattle.Drawing;
using System.Windows.Media.Imaging;


namespace MidwayBattle.Drawing
{
    public class Drawings
    {

        public void SplashScreen()
        {
            SplashScreen splashScreen = new SplashScreen("/img/SplashScreen.png");
            splashScreen.Show(true);
            //lengthens the time of the splash screen
            System.Threading.Thread.Sleep(CustomDefines.SPLASHSCREENTIMER);
            splashScreen.Close(TimeSpan.Zero);
        }
        
        //-------------------------------------------------------------------------------------------------------------------
        public void SetBGImages(Image bg1, Image bg2)
        {
            Grid.SetColumnSpan(bg1, CustomDefines.NBSQUARESX);
            Grid.SetRowSpan(bg1, CustomDefines.NBSQUARESY);

            Grid.SetColumnSpan(bg2, CustomDefines.NBSQUARESX);
            Grid.SetRowSpan(bg2, CustomDefines.NBSQUARESY);

        }
        //-------------------------------------------------------------------------------------------------------------------
        public void GenerateGridDefinitions(Grid p1, Grid p2)
        {
            GridLength glLines = new GridLength((double)1 / (double)CustomDefines.NBSQUARESY, GridUnitType.Star);

            for (int i = 0; i < CustomDefines.NBSQUARESY; i++)
            {
                RowDefinition rdef1 = new RowDefinition();
                RowDefinition rdef2 = new RowDefinition();
                rdef1.Height = glLines;
                rdef2.Height = glLines;

                p1.RowDefinitions.Add(rdef1);
                p2.RowDefinitions.Add(rdef2);
            }

            GridLength glColumns = new GridLength((double)1 / (double)CustomDefines.NBSQUARESY, GridUnitType.Star);

            for (int j = 0; j < CustomDefines.NBSQUARESX; j++)
            {
                ColumnDefinition cdef1 = new ColumnDefinition();
                ColumnDefinition cdef2 = new ColumnDefinition();
                cdef1.Width = glColumns;
                cdef2.Width = glColumns;

                p1.ColumnDefinitions.Add(cdef1);
                p2.ColumnDefinitions.Add(cdef2);
            }

        }
        //-------------------------------------------------------------------------------------------------------------------
        public void DrawLegends(Grid GenGrid)
        {
            // Création et ajout des 7 sous-grids
            Grid H1Top = new Grid();
            Grid H2Top = new Grid();
            Grid H1Bott = new Grid();
            Grid H2Bott = new Grid();

            Grid V1Left = new Grid();
            Grid V12 = new Grid();
            Grid V2Right = new Grid();

            // TEMP - TODEL
            H1Top.ShowGridLines = H2Top.ShowGridLines = H1Bott.ShowGridLines = H2Bott.ShowGridLines = V1Left.ShowGridLines = V12.ShowGridLines = V2Right.ShowGridLines = false;



            H1Top.HorizontalAlignment = H2Top.HorizontalAlignment = H1Bott.HorizontalAlignment = H2Bott.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            V1Left.HorizontalAlignment = V12.HorizontalAlignment = V2Right.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

            H1Top.VerticalAlignment = H2Top.VerticalAlignment = H1Bott.VerticalAlignment = H2Bott.VerticalAlignment = V1Left.VerticalAlignment
                    = V12.VerticalAlignment = V2Right.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

            H1Top.Width = H2Top.Width = H1Bott.Width = H2Bott.Width = V1Left.Width = V12.Width = V2Right.Width = Double.NaN;


            // DEFINITIONS pour les 4 sous-grilles de légendes horizontales
            GridLength glLinesHor = new GridLength(1, GridUnitType.Star);
            GridLength glColumnsHor = new GridLength((double)1 / (double)CustomDefines.NBSQUARESX, GridUnitType.Star);

            for (int j = 0; j < CustomDefines.NBSQUARESX; j++)
            {
                ColumnDefinition cdefH1T = new ColumnDefinition();
                ColumnDefinition cdefH2T = new ColumnDefinition();
                ColumnDefinition cdefH1B = new ColumnDefinition();
                ColumnDefinition cdefH2B = new ColumnDefinition();
                cdefH1T.Width = cdefH2T.Width = cdefH1B.Width = cdefH2B.Width = glColumnsHor;
                H1Top.ColumnDefinitions.Add(cdefH1T);
                H2Top.ColumnDefinitions.Add(cdefH2T);
                H1Bott.ColumnDefinitions.Add(cdefH1B);
                H2Bott.ColumnDefinitions.Add(cdefH2B);
            }

            RowDefinition rdefH1T = new RowDefinition();
            RowDefinition rdefH2T = new RowDefinition();
            RowDefinition rdefH1B = new RowDefinition();
            RowDefinition rdefH2B = new RowDefinition();
            rdefH1T.Height = rdefH2T.Height = rdefH1B.Height = rdefH2B.Height = glLinesHor;
            H1Top.RowDefinitions.Add(rdefH1T);
            H2Top.RowDefinitions.Add(rdefH2T);
            H1Bott.RowDefinitions.Add(rdefH1B);
            H2Bott.RowDefinitions.Add(rdefH2B);



            // DEFINITIONS pour les 3 sous-grilles de légendes verticales
            GridLength glLinesVer = new GridLength((double)1 / (double)CustomDefines.NBSQUARESY, GridUnitType.Star);
            GridLength glColumnsVer = new GridLength(1, GridUnitType.Star);

            for (int j = 0; j < CustomDefines.NBSQUARESX; j++)
            {

                RowDefinition rdefV1L = new RowDefinition();
                RowDefinition rdefV12 = new RowDefinition();
                RowDefinition rdefV2R = new RowDefinition();

                rdefV1L.Height = rdefV12.Height = rdefV2R.Height = glLinesVer;
                V1Left.RowDefinitions.Add(rdefV1L);
                V12.RowDefinitions.Add(rdefV12);
                V2Right.RowDefinitions.Add(rdefV2R);
            }

            ColumnDefinition cdefV1L = new ColumnDefinition();
            ColumnDefinition cdefV12 = new ColumnDefinition();
            ColumnDefinition cdefV2R = new ColumnDefinition();
            cdefV1L.Width = cdefV12.Width = cdefV2R.Width = glColumnsVer;
            V1Left.ColumnDefinitions.Add(cdefV1L);
            V12.ColumnDefinitions.Add(cdefV12);
            V2Right.ColumnDefinitions.Add(cdefV2R);





            // Ajout des sous-grilles à la grille principale
            GenGrid.Children.Add(H1Top);
            Grid.SetColumn(H1Top, CustomDefines.GENGRID_LGD_LEFT_COLUMN + 1);  //leftcol +1
            Grid.SetRow(H1Top, CustomDefines.GENGRID_LGD_TOPROW); // toprow

            GenGrid.Children.Add(H2Top);
            Grid.SetColumn(H2Top, CustomDefines.GENGRID_LGD_MIDDLE_COLUMN + 1); //middlecol+1
            Grid.SetRow(H2Top, CustomDefines.GENGRID_LGD_TOPROW);  // toprow

            GenGrid.Children.Add(H1Bott);
            Grid.SetColumn(H1Bott, CustomDefines.GENGRID_LGD_LEFT_COLUMN + 1); // leftcol+1
            Grid.SetRow(H1Bott, CustomDefines.GENGRID_LGD_BOTTOMROW); // bottrow

            GenGrid.Children.Add(H2Bott);
            Grid.SetColumn(H2Bott, CustomDefines.GENGRID_LGD_MIDDLE_COLUMN + 1); //middlecol +1
            Grid.SetRow(H2Bott, CustomDefines.GENGRID_LGD_BOTTOMROW); // bottrow

            GenGrid.Children.Add(V1Left);
            Grid.SetColumn(V1Left, CustomDefines.GENGRID_LGD_LEFT_COLUMN); // leftcol
            Grid.SetRow(V1Left, CustomDefines.GENGRID_LGD_TOPROW + 1); // toprow +1

            GenGrid.Children.Add(V12);
            Grid.SetColumn(V12, CustomDefines.GENGRID_LGD_MIDDLE_COLUMN); // midcol
            Grid.SetRow(V12, CustomDefines.GENGRID_LGD_TOPROW + 1); // toprow +1

            GenGrid.Children.Add(V2Right);
            Grid.SetColumn(V2Right, CustomDefines.GENGRID_LGD_RIGHT_COLUMN); // rightcol
            Grid.SetRow(V2Right, CustomDefines.GENGRID_LGD_TOPROW + 1); // toprow +1


            // Légendes HORIZONTALES (Lettres)
            for (int i = 0; i < CustomDefines.NBSQUARESX; i++)
            {
                Label labH1T = new Label();
                Label labH2T = new Label();
                Label labH1B = new Label();
                Label labH2B = new Label();
                labH1T.Content = labH2T.Content = labH1B.Content = labH2B.Content = CustomDefines.TabLettres[i];
                labH1T.Tag = "Legend_H1Top_" + i;
                labH2T.Tag = "Legend_H2Top_" + i;
                labH1B.Tag = "Legend_H1Bot_" + i;
                labH2B.Tag = "Legend_H2Bot_" + i;
                labH1T.Width = labH2T.Width = labH1B.Width = labH2B.Width = 20;

                H1Top.Children.Add(labH1T);
                Grid.SetRow(labH1T, 0);
                Grid.SetColumn(labH1T, i);

                H2Top.Children.Add(labH2T);
                Grid.SetRow(labH2T, 0);
                Grid.SetColumn(labH2T, i);

                H1Bott.Children.Add(labH1B);
                Grid.SetRow(labH1B, 0);
                Grid.SetColumn(labH1B, i);

                H2Bott.Children.Add(labH2B);
                Grid.SetRow(labH2B, 0);
                Grid.SetColumn(labH2B, i);
            }


            // Légendes VERTICALES (Chiffres)
            for (int j = 0; j < CustomDefines.NBSQUARESY; j++)
            {
                Label labV1L = new Label();
                Label labV12 = new Label();
                Label labV2R = new Label();
                labV1L.Content = labV12.Content = labV2R.Content = j + 1;
                labV1L.Tag = "Legend_V1Leftp_" + j;
                labV12.Tag = "Legend_V12_" + j;
                labV2R.Tag = "Legend_V2Right_" + j;

                V1Left.Children.Add(labV1L);
                Grid.SetRow(labV1L, j);
                Grid.SetColumn(labV1L, 0);

                V12.Children.Add(labV12);
                Grid.SetRow(labV12, j);
                Grid.SetColumn(labV12, 0);

                V2Right.Children.Add(labV2R);
                Grid.SetRow(labV2R, j);
                Grid.SetColumn(labV2R, 0);

            }




        }
        //-------------------------------------------------------------------------------------------------------------------
        public void DrawSquares(Grid p1, Grid p2, MouseEventHandler leave, MouseEventHandler enter, MouseWheelEventHandler wheel, MouseButtonEventHandler buttDown)
        {
            for (int player = 1; player <= CustomDefines.PLAYERS_AMOUNT; player++)
            {
                for (int ligne = 0; ligne < CustomDefines.NBSQUARESY; ligne++)
                {
                    for (int colonne = 0; colonne < CustomDefines.NBSQUARESX; colonne++)
                    {
                        Rectangle square = new Rectangle();

                        square.Stroke = CustomDefines.SQUARE_STROKE_BRUSH;
                        square.StrokeThickness = CustomDefines.SQUARE_STROKE_THICKNESS;
                        square.Stretch = Stretch.Fill;

                        square.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_SQUARE, Keys.SQUARE, colonne, ligne, player);

                        square.Name = "Square_PL" + player.ToString() + "_X" + Convert.ToString(colonne) + "_Y" + Convert.ToString(ligne);
                        square.HorizontalAlignment = HorizontalAlignment.Stretch;
                        square.VerticalAlignment = VerticalAlignment.Stretch;

                        // Ce qui suit est nécessaire pour que les cases soient complètement cliquables (et non
                        // seulement leur bordure) 
                        square.Fill = Brushes.Transparent;

                        square.MouseLeave += leave;
                        square.MouseEnter += enter;
                        square.MouseWheel += wheel;
                        square.MouseLeftButtonDown += buttDown;

                        switch (player)
                        {
                            case 1: p1.Children.Add(square); break;
                            case 2: p2.Children.Add(square); break;
                        } // end switch

                        Grid.SetRow(square, ligne);
                        Grid.SetColumn(square, colonne);
                    } // end for colonne
                } // end for ligne
            } // end for player
        }

        //-------------------------------------------------------------------------------------------------------------------
        public void InitCommandsPanel(int mode, RoutedEventHandler shipBtnClick, GroupBox playBox, GroupBox placeBox)
        {
            CustomDefines defines = new CustomDefines();

            Grid PlaceCmdGrid = new Grid();
            Grid PlayCmdGrid = new Grid();

            switch (mode)
            {
                case CustomDefines.DRAW_COMMANDS_PLACEMENT:
                    {
                        PlaceCmdGrid.Children.Clear(); // vidage de la Grid

                        // 1: Détermination des RowDefinition et ColumnDefinition

                        GridLength glLines = new GridLength(CustomDefines.GRID_PLAC_LARGEROW, GridUnitType.Star);
                        GridLength glSpace = new GridLength(CustomDefines.GRID_PLAC_SPACEROW_PIX, GridUnitType.Pixel);

                        RowDefinition rdefLegend = new RowDefinition();
                        RowDefinition rdefSpace1 = new RowDefinition();
                        RowDefinition rdefBtnL1 = new RowDefinition();
                        RowDefinition rdefSpace2 = new RowDefinition();
                        RowDefinition rdefBtnL2 = new RowDefinition();
                        RowDefinition rdefSpace3 = new RowDefinition();
                        RowDefinition rdefBtnL3 = new RowDefinition();
                        RowDefinition rdefSpace4 = new RowDefinition();
                        RowDefinition rdefPlayBtn = new RowDefinition();

                        rdefLegend.Height = rdefBtnL1.Height = rdefBtnL2.Height = rdefBtnL3.Height = rdefPlayBtn.Height = glLines;
                        rdefSpace1.Height = rdefSpace2.Height = rdefSpace3.Height = rdefSpace4.Height = glSpace;

                        GridLength glColumnsLarge = new GridLength(CustomDefines.GRID_PLAC_LARGECOLUMNSIZE, GridUnitType.Star);
                        GridLength glColumnsSmall = new GridLength(CustomDefines.GRID_PLAC_SMALLCOLUMNSIZE, GridUnitType.Star);

                        ColumnDefinition cdef0 = new ColumnDefinition();
                        ColumnDefinition cdef1 = new ColumnDefinition();
                        ColumnDefinition cdef2 = new ColumnDefinition();
                        ColumnDefinition cdef3 = new ColumnDefinition();
                        ColumnDefinition cdef4 = new ColumnDefinition();
                        ColumnDefinition cdef5 = new ColumnDefinition();

                        cdef0.Width = cdef2.Width = cdef4.Width = glColumnsLarge;
                        cdef1.Width = cdef3.Width = cdef5.Width = glColumnsSmall;

                        PlaceCmdGrid.RowDefinitions.Add(rdefLegend);
                        PlaceCmdGrid.RowDefinitions.Add(rdefSpace1);
                        PlaceCmdGrid.RowDefinitions.Add(rdefBtnL1);
                        PlaceCmdGrid.RowDefinitions.Add(rdefSpace2);
                        PlaceCmdGrid.RowDefinitions.Add(rdefBtnL2);
                        PlaceCmdGrid.RowDefinitions.Add(rdefSpace3);
                        PlaceCmdGrid.RowDefinitions.Add(rdefBtnL3);
                        PlaceCmdGrid.RowDefinitions.Add(rdefSpace4);
                        PlaceCmdGrid.RowDefinitions.Add(rdefPlayBtn);

                        PlaceCmdGrid.ColumnDefinitions.Add(cdef0);
                        PlaceCmdGrid.ColumnDefinitions.Add(cdef1);
                        PlaceCmdGrid.ColumnDefinitions.Add(cdef2);
                        PlaceCmdGrid.ColumnDefinitions.Add(cdef3);
                        PlaceCmdGrid.ColumnDefinitions.Add(cdef4);
                        PlaceCmdGrid.ColumnDefinitions.Add(cdef5);

                        // 2: Initialiation des Label

                        Label nbrRestCol1Titre = new Label();
                        Label nbrRestCol3Titre = new Label();
                        Label nbrRestCol5Titre = new Label();

                        Label nbrRestBat1Value = new Label();
                        Label nbrRestBat2Value = new Label();
                        Label nbrRestBat3Value = new Label();
                        Label nbrRestBat4Value = new Label();
                        Label nbrRestBat5Value = new Label();

                        Label nbrRestMinesValue = new Label();

                        nbrRestCol1Titre.Content = nbrRestCol3Titre.Content = nbrRestCol5Titre.Content = CustomDefines.GRID_PLAC_STR_NBLEFT;

                        nbrRestBat1Value.Content = nbrRestBat2Value.Content = nbrRestBat3Value.Content = nbrRestBat4Value.Content = nbrRestBat5Value.Content = nbrRestMinesValue.Content = CustomDefines.GRID_PLAC_STR_ZEROVAL;

                        Label minesTitre = new Label();
                        minesTitre.Content = CustomDefines.GRID_PLAC_STR_MINESTITLE;
                        minesTitre.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;

                        nbrRestBat1Value.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.BAT1LEFT, -1, -1, -1);
                        nbrRestBat2Value.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.BAT2LEFT, -1, -1, -1);
                        nbrRestBat3Value.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.BAT3LEFT, -1, -1, -1);
                        nbrRestBat4Value.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.BAT4LEFT, -1, -1, -1);
                        nbrRestBat5Value.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.BAT5LEFT, -1, -1, -1);
                        nbrRestMinesValue.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.MINESLEFT, -1, -1, -1);



                        // 3: Initialisation des Button

                        Button bat1Btn = new Button();
                        Button bat2Btn = new Button();
                        Button bat3Btn = new Button();
                        Button bat4Btn = new Button();
                        Button bat5Btn = new Button();
                        Button batResetBtn = new Button();

                        Button placeMinesBtn = new Button();
                        Button minResetBtn = new Button();

                        Button playBtn = new Button();

                        bat1Btn.Content = String.Format("{0} ({1})", CustomDefines.SHIP_CARRIER_NAME, CustomDefines.SHIP_CARRIER_SIZE);
                        bat1Btn.Click += shipBtnClick;
                        bat1Btn.Background = defines.SHIP_CARRIER_COLOR;
                        bat1Btn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.BAT1PLBTN, -1, -1, -1);

                        bat2Btn.Content = String.Format("{0} ({1})", CustomDefines.SHIP_BATTLESHIP_NAME, CustomDefines.SHIP_BATTLESHIP_SIZE);
                        bat2Btn.Click += shipBtnClick;
                        bat2Btn.Background = defines.SHIP_BATTLESHIP_COLOR;
                        bat2Btn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.BAT2PLBTN, -1, -1, -1);

                        bat3Btn.Content = String.Format("{0} ({1})", CustomDefines.SHIP_CRUISER_NAME, CustomDefines.SHIP_CRUISER_SIZE);
                        bat3Btn.Click += shipBtnClick;
                        bat3Btn.Background = defines.SHIP_CRUISER_COLOR;
                        bat3Btn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.BAT3PLBTN, -1, -1, -1);

                        bat4Btn.Content = String.Format("{0} ({1})", CustomDefines.SHIP_DESTROYER_NAME, CustomDefines.SHIP_DESTROYER_SIZE);
                        bat4Btn.Click += shipBtnClick;
                        bat4Btn.Background = defines.SHIP_DESTROYER_COLOR;
                        bat4Btn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.BAT4PLBTN, -1, -1, -1);

                        bat5Btn.Content = String.Format("{0} ({1})", CustomDefines.SHIP_SUBMARINE_NAME, CustomDefines.SHIP_SUBMARINE_SIZE);
                        bat5Btn.Click += shipBtnClick;
                        bat5Btn.Background = defines.SHIP_SUBMARINE_COLOR;
                        bat5Btn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.BAT5PLBTN, -1, -1, -1);

                        batResetBtn.Content = CustomDefines.GRID_PLAC_STR_REMOVESHIPS;
                        batResetBtn.Click += shipBtnClick;
                        batResetBtn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.RESETSHIPBTN, -1, -1, -1);

                        placeMinesBtn.Content = CustomDefines.GRID_PLAC_STR_PLACEMINES;
                        placeMinesBtn.Click += shipBtnClick;
                        placeMinesBtn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.PLACEMINESBTN, -1, -1, -1);

                        minResetBtn.Content = CustomDefines.GRID_PLAC_STR_REMOVEMINES;
                        minResetBtn.Click += shipBtnClick;
                        minResetBtn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.MINRESETBTN, -1, -1, -1);

                        playBtn.Content = CustomDefines.GRID_PLAC_STR_PLAYBTN;
                        playBtn.FontWeight = CustomDefines.GRID_PLAC_PLAYBTN_FONTWEIGHT;
                        playBtn.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_BUTTON, Keys.PLAYBTN, -1, -1, -1);
                        playBtn.IsEnabled = false; // PlayBtn est désactivé par défaut, le temps que le joueur place ses navires et ses mines
                        playBtn.BorderBrush = CustomDefines.GRID_PLAC_GOCOLOR;
                        playBtn.Foreground = Brushes.LightSlateGray;
                        playBtn.ToolTip = CustomDefines.TOOLTIP_PLAYBTN_DISABLED;
                        playBtn.SetValue(ToolTipService.ShowOnDisabledProperty, true); // Force l'affichage du tooltip si le bouton play est désactivé
                        playBtn.Click += shipBtnClick;

                        batResetBtn.BorderBrush = CustomDefines.GRID_PLAC_WARNINGCOLOR;
                        batResetBtn.IsEnabled = false; // Desactive ausi longtemps qu'au moins un navire n'a pas été placé

                        minResetBtn.BorderBrush = CustomDefines.GRID_PLAC_WARNINGCOLOR;
                        minResetBtn.IsEnabled = false; // Desactive ausi longtemps qu'au moins une mine n'a pas été placée


                        // 4: Placement des composants titres

                        PlaceCmdGrid.Children.Add(nbrRestCol1Titre);
                        Grid.SetRow(nbrRestCol1Titre, CustomDefines.GRID_PLAC_TITLELINE);
                        Grid.SetColumn(nbrRestCol1Titre, CustomDefines.GRID_PLAC_VALUES_FIRSTCOL);

                        PlaceCmdGrid.Children.Add(nbrRestCol3Titre);
                        Grid.SetRow(nbrRestCol3Titre, CustomDefines.GRID_PLAC_TITLELINE);
                        Grid.SetColumn(nbrRestCol3Titre, CustomDefines.GRID_PLAC_VALUES_SECONDCOL);

                        PlaceCmdGrid.Children.Add(nbrRestCol5Titre);
                        Grid.SetRow(nbrRestCol5Titre, CustomDefines.GRID_PLAC_TITLELINE);
                        Grid.SetColumn(nbrRestCol5Titre, CustomDefines.GRID_PLAC_VALUES_MINESCOL);

                        PlaceCmdGrid.Children.Add(minesTitre);
                        Grid.SetRow(minesTitre, CustomDefines.GRID_PLAC_TITLELINE);
                        Grid.SetColumn(minesTitre, CustomDefines.GRID_PLAC_BUTTONS_MINESCOL);


                        // 5: Placement des composants boutons

                        PlaceCmdGrid.Children.Add(bat1Btn);
                        Grid.SetRow(bat1Btn, CustomDefines.GRID_PLAC_ITEMSLINE1);
                        Grid.SetColumn(bat1Btn, CustomDefines.GRID_PLAC_BUTTONS_FIRSTCOL);

                        PlaceCmdGrid.Children.Add(bat2Btn);
                        Grid.SetRow(bat2Btn, CustomDefines.GRID_PLAC_ITEMSLINE2);
                        Grid.SetColumn(bat2Btn, CustomDefines.GRID_PLAC_BUTTONS_FIRSTCOL);

                        PlaceCmdGrid.Children.Add(bat3Btn);
                        Grid.SetRow(bat3Btn, CustomDefines.GRID_PLAC_ITEMSLINE3);
                        Grid.SetColumn(bat3Btn, CustomDefines.GRID_PLAC_BUTTONS_FIRSTCOL);

                        PlaceCmdGrid.Children.Add(bat4Btn);
                        Grid.SetRow(bat4Btn, CustomDefines.GRID_PLAC_ITEMSLINE1);
                        Grid.SetColumn(bat4Btn, CustomDefines.GRID_PLAC_BUTTONS_SECONDCOL);

                        PlaceCmdGrid.Children.Add(bat5Btn);
                        Grid.SetRow(bat5Btn, CustomDefines.GRID_PLAC_ITEMSLINE2);
                        Grid.SetColumn(bat5Btn, CustomDefines.GRID_PLAC_BUTTONS_SECONDCOL);

                        PlaceCmdGrid.Children.Add(batResetBtn);
                        Grid.SetRow(batResetBtn, CustomDefines.GRID_PLAC_ITEMSLINE3);
                        Grid.SetColumn(batResetBtn, CustomDefines.GRID_PLAC_BUTTONS_SECONDCOL);

                        PlaceCmdGrid.Children.Add(placeMinesBtn);
                        Grid.SetRow(placeMinesBtn, CustomDefines.GRID_PLAC_ITEMSLINE1);
                        Grid.SetColumn(placeMinesBtn, CustomDefines.GRID_PLAC_BUTTONS_MINESCOL);

                        PlaceCmdGrid.Children.Add(minResetBtn);
                        Grid.SetRow(minResetBtn, CustomDefines.GRID_PLAC_ITEMSLINE2);
                        Grid.SetColumn(minResetBtn, CustomDefines.GRID_PLAC_BUTTONS_MINESCOL);

                        PlaceCmdGrid.Children.Add(playBtn);
                        Grid.SetRow(playBtn, CustomDefines.GRID_PLAC_PLAYBTNLINE);
                        Grid.SetColumn(playBtn, CustomDefines.GRID_PLAC_BUTTONS_FIRSTCOL);
                        Grid.SetColumnSpan(playBtn, CustomDefines.GRID_PLAC_PLAYBTN_HORIZSPAN);

                        // 6: Placement des Label des valeurs

                        PlaceCmdGrid.Children.Add(nbrRestBat1Value);
                        Grid.SetRow(nbrRestBat1Value, CustomDefines.GRID_PLAC_ITEMSLINE1);
                        Grid.SetColumn(nbrRestBat1Value, CustomDefines.GRID_PLAC_VALUES_FIRSTCOL);

                        PlaceCmdGrid.Children.Add(nbrRestBat2Value);
                        Grid.SetRow(nbrRestBat2Value, CustomDefines.GRID_PLAC_ITEMSLINE2);
                        Grid.SetColumn(nbrRestBat2Value, CustomDefines.GRID_PLAC_VALUES_FIRSTCOL);

                        PlaceCmdGrid.Children.Add(nbrRestBat3Value);
                        Grid.SetRow(nbrRestBat3Value, CustomDefines.GRID_PLAC_ITEMSLINE3);
                        Grid.SetColumn(nbrRestBat3Value, CustomDefines.GRID_PLAC_VALUES_FIRSTCOL);

                        PlaceCmdGrid.Children.Add(nbrRestBat4Value);
                        Grid.SetRow(nbrRestBat4Value, CustomDefines.GRID_PLAC_ITEMSLINE1);
                        Grid.SetColumn(nbrRestBat4Value, CustomDefines.GRID_PLAC_VALUES_SECONDCOL);

                        PlaceCmdGrid.Children.Add(nbrRestBat5Value);
                        Grid.SetRow(nbrRestBat5Value, CustomDefines.GRID_PLAC_ITEMSLINE2);
                        Grid.SetColumn(nbrRestBat5Value, CustomDefines.GRID_PLAC_VALUES_SECONDCOL);

                        PlaceCmdGrid.Children.Add(nbrRestMinesValue);
                        Grid.SetRow(nbrRestMinesValue, CustomDefines.GRID_PLAC_ITEMSLINE1);
                        Grid.SetColumn(nbrRestMinesValue, CustomDefines.GRID_PLAC_VALUES_MINESCOL);

                        // 7: Affichage de la grid de placement après masquage de celle de jeu (si affichée)

                        playBox.Visibility = System.Windows.Visibility.Hidden;
                        placeBox.Content = PlaceCmdGrid;
                        placeBox.Visibility = System.Windows.Visibility.Visible;


                        break;
                    }

                case CustomDefines.DRAW_COMMANDS_PLAY:
                    {
                        PlayCmdGrid.Children.Clear(); // vidage de la Grid

                        // 1: Détermination des RowDefinition et ColumnDefinition

                        GridLength glLines = new GridLength(CustomDefines.GRID_PLAY_LARGEROW, GridUnitType.Star);
                        GridLength glSpace = new GridLength(CustomDefines.GRID_PLAY_SPACEROW_PIX, GridUnitType.Pixel);

                        RowDefinition rdefLegend = new RowDefinition();
                        RowDefinition rdefSpace1 = new RowDefinition();
                        RowDefinition rdefItemsL1 = new RowDefinition();
                        RowDefinition rdefSpace2 = new RowDefinition();
                        RowDefinition rdefItemsL2 = new RowDefinition();
                        RowDefinition rdefSpace3 = new RowDefinition();
                        RowDefinition rdefItemsL3 = new RowDefinition();
                        RowDefinition rdefSpace4 = new RowDefinition();
                        RowDefinition rdefItemsL4 = new RowDefinition();
                        RowDefinition rdefSpace5 = new RowDefinition();

                        rdefLegend.Height = rdefSpace1.Height = rdefItemsL2.Height = rdefItemsL3.Height = rdefItemsL4.Height = glLines;
                        rdefSpace1.Height = rdefSpace2.Height = rdefSpace3.Height = rdefSpace4.Height = rdefSpace5.Height = glSpace;

                        GridLength glColumn0 = new GridLength(CustomDefines.GRID_PLAY_COL0_SIZE, GridUnitType.Star);
                        GridLength glColumnsSpace = new GridLength(CustomDefines.GRID_PLAY_SPACECOLSIZE, GridUnitType.Star);
                        GridLength glColumn1 = new GridLength(CustomDefines.GRID_PLAY_COL1_SIZE, GridUnitType.Star);
                        GridLength glColumn2 = new GridLength(CustomDefines.GRID_PLAY_COL2_SIZE, GridUnitType.Star);
                        GridLength glColumn3 = new GridLength(CustomDefines.GRID_PLAY_COL3_SIZE, GridUnitType.Star);
                        GridLength glColumn4 = new GridLength(CustomDefines.GRID_PLAY_COL4_SIZE, GridUnitType.Star);

                        ColumnDefinition cdef0 = new ColumnDefinition();
                        ColumnDefinition cdefSpace = new ColumnDefinition();
                        ColumnDefinition cdef1 = new ColumnDefinition();
                        ColumnDefinition cdef2 = new ColumnDefinition();
                        ColumnDefinition cdef3 = new ColumnDefinition();
                        ColumnDefinition cdef4 = new ColumnDefinition();

                        cdef0.Width = glColumn0;
                        cdefSpace.Width = glColumnsSpace;
                        cdef1.Width = glColumn1;
                        cdef2.Width = glColumn2;
                        cdef3.Width = glColumn3;
                        cdef4.Width = glColumn4;

                        PlayCmdGrid.RowDefinitions.Add(rdefLegend);
                        PlayCmdGrid.RowDefinitions.Add(rdefSpace1);
                        PlayCmdGrid.RowDefinitions.Add(rdefItemsL1);
                        PlayCmdGrid.RowDefinitions.Add(rdefSpace2);
                        PlayCmdGrid.RowDefinitions.Add(rdefItemsL2);
                        PlayCmdGrid.RowDefinitions.Add(rdefSpace3);
                        PlayCmdGrid.RowDefinitions.Add(rdefItemsL3);
                        PlayCmdGrid.RowDefinitions.Add(rdefSpace4);
                        PlayCmdGrid.RowDefinitions.Add(rdefItemsL4);
                        PlayCmdGrid.RowDefinitions.Add(rdefSpace5);

                        PlayCmdGrid.ColumnDefinitions.Add(cdef0);
                        PlayCmdGrid.ColumnDefinitions.Add(cdefSpace);
                        PlayCmdGrid.ColumnDefinitions.Add(cdef1);
                        PlayCmdGrid.ColumnDefinitions.Add(cdef2);
                        PlayCmdGrid.ColumnDefinitions.Add(cdef3);
                        PlayCmdGrid.ColumnDefinitions.Add(cdef4);

                        // 2: Initialisation des Label

                        Label actionsTitre = new Label();
                        Label infosTitre = new Label();

                        Label shipsSunkByYouLabel = new Label();
                        Label shipsSunkByOpponentLabel = new Label();
                        Label tourNbrLabel = new Label();
                        Label yourScoreLabel = new Label();
                        Label opponentScoreLabel = new Label();

                        Label shipsSunkByYouValue = new Label();
                        Label shipsSunkByOpponentValue = new Label();
                        Label tourNbrValue = new Label();
                        Label yourScoreValue = new Label();
                        Label opponentScoreValue = new Label();

                        actionsTitre.Content = CustomDefines.GRID_PLAY_STR_ACTIONSTITLE;
                        infosTitre.Content = CustomDefines.GRID_PLAY_STR_INFOSTITLE;

                        shipsSunkByYouLabel.Content = CustomDefines.GRID_PLAY_STR_SHIPSSUNKBYYOU;
                        shipsSunkByOpponentLabel.Content = CustomDefines.GRID_PLAY_STR_SHIPSSUNKBYOPPONENT;
                        tourNbrLabel.Content = CustomDefines.GRID_PLAY_STR_TOURNBR;
                        yourScoreLabel.Content = CustomDefines.GRID_PLAY_STR_YOURSCORE;
                        opponentScoreLabel.Content = CustomDefines.GRID_PLAY_STR_OPPONENTSCORE;
                        shipsSunkByYouValue.Content = shipsSunkByOpponentValue.Content = tourNbrValue.Content = yourScoreValue.Content = opponentScoreValue.Content = CustomDefines.GRID_PLAY_STR_ZEROVAL;

                        infosTitre.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;

                        shipsSunkByYouValue.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.SHIPS_SUNK_BY_YOU, -1, -1, -1);
                        shipsSunkByOpponentValue.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.SHIPS_SUNK_BY_OPPONENT, -1, -1, -1);
                        tourNbrValue.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.TOUR_NBR, -1, -1, -1);
                        yourScoreValue.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.YOUR_SCORE, -1, -1, -1);
                        opponentScoreValue.Tag = new TagComponentProperties(TagComponentProperties.CompTypes.TYPE_LABEL, Keys.OPPONENT_SCORE, -1, -1, -1);




                        // 3: Initialisation des boutons

                        Button fireBtn = new Button();
                        Button radarBtn = new Button();
                        Button sabotageBtn = new Button();

                        fireBtn.Content = CustomDefines.GRID_PLAY_STR_FIREBTN;
                        radarBtn.Content = CustomDefines.GRID_PLAY_STR_RADARBTN;
                        sabotageBtn.Content = CustomDefines.GRID_PLAY_STR_SABOTAGEBTN;

                        fireBtn.FontWeight = CustomDefines.GRID_PLAY_FIREBTN_FONTWEIGHT;

                        fireBtn.BorderBrush = CustomDefines.GRID_PLAY_WARNINGCOLOR;
                        fireBtn.Foreground = CustomDefines.GRID_PLAY_WARNINGCOLOR;


                        // 4: Placement des composants titres et libellés fixes

                        PlayCmdGrid.Children.Add(actionsTitre);
                        Grid.SetRow(actionsTitre, CustomDefines.GRID_PLAY_TITLELINE);
                        Grid.SetColumn(actionsTitre, CustomDefines.GRID_PLAY_BUTTONSCOL);

                        PlayCmdGrid.Children.Add(infosTitre);
                        Grid.SetRow(infosTitre, CustomDefines.GRID_PLAY_TITLELINE);
                        Grid.SetColumn(infosTitre, CustomDefines.GRID_PLAY_LABELSCOL1);

                        PlayCmdGrid.Children.Add(shipsSunkByYouLabel);
                        Grid.SetRow(shipsSunkByYouLabel, CustomDefines.GRID_PLAY_LINE1);
                        Grid.SetColumn(shipsSunkByYouLabel, CustomDefines.GRID_PLAY_LABELSCOL1);
                        Grid.SetColumnSpan(shipsSunkByYouLabel, 2);

                        PlayCmdGrid.Children.Add(shipsSunkByOpponentLabel);
                        Grid.SetRow(shipsSunkByOpponentLabel, CustomDefines.GRID_PLAY_LINE2);
                        Grid.SetColumn(shipsSunkByOpponentLabel, CustomDefines.GRID_PLAY_LABELSCOL1);
                        Grid.SetColumnSpan(shipsSunkByOpponentLabel, 2);

                        PlayCmdGrid.Children.Add(tourNbrLabel);
                        Grid.SetRow(tourNbrLabel, CustomDefines.GRID_PLAY_LINE3);
                        Grid.SetColumn(tourNbrLabel, CustomDefines.GRID_PLAY_LABELSCOL1);

                        PlayCmdGrid.Children.Add(yourScoreLabel);
                        Grid.SetRow(yourScoreLabel, CustomDefines.GRID_PLAY_LINE4);
                        Grid.SetColumn(yourScoreLabel, CustomDefines.GRID_PLAY_LABELSCOL1);

                        PlayCmdGrid.Children.Add(opponentScoreLabel);
                        Grid.SetRow(opponentScoreLabel, CustomDefines.GRID_PLAY_LINE4);
                        Grid.SetColumn(opponentScoreLabel, CustomDefines.GRID_PLAY_LABELSCOL3);


                        // 5: Placement des composants boutons

                        PlayCmdGrid.Children.Add(fireBtn);
                        Grid.SetRow(fireBtn, CustomDefines.GRID_PLAY_LINE1);
                        Grid.SetColumn(fireBtn, CustomDefines.GRID_PLAY_BUTTONSCOL);

                        PlayCmdGrid.Children.Add(radarBtn);
                        Grid.SetRow(radarBtn, CustomDefines.GRID_PLAY_LINE2);
                        Grid.SetColumn(radarBtn, CustomDefines.GRID_PLAY_BUTTONSCOL);

                        PlayCmdGrid.Children.Add(sabotageBtn);
                        Grid.SetRow(sabotageBtn, CustomDefines.GRID_PLAY_LINE3);
                        Grid.SetColumn(sabotageBtn, CustomDefines.GRID_PLAY_BUTTONSCOL);


                        // 6: Placement des Label des valeurs

                        PlayCmdGrid.Children.Add(shipsSunkByYouValue);
                        Grid.SetRow(shipsSunkByYouValue, CustomDefines.GRID_PLAY_LINE1);
                        Grid.SetColumn(shipsSunkByYouValue, CustomDefines.GRID_PLAY_LABELSCOL3);

                        PlayCmdGrid.Children.Add(shipsSunkByOpponentValue);
                        Grid.SetRow(shipsSunkByOpponentValue, CustomDefines.GRID_PLAY_LINE2);
                        Grid.SetColumn(shipsSunkByOpponentValue, CustomDefines.GRID_PLAY_LABELSCOL3);

                        PlayCmdGrid.Children.Add(tourNbrValue);
                        Grid.SetRow(tourNbrValue, CustomDefines.GRID_PLAY_LINE3);
                        Grid.SetColumn(tourNbrValue, CustomDefines.GRID_PLAY_LABELSCOL2);

                        PlayCmdGrid.Children.Add(yourScoreValue);
                        Grid.SetRow(yourScoreValue, CustomDefines.GRID_PLAY_LINE4);
                        Grid.SetColumn(yourScoreValue, CustomDefines.GRID_PLAY_LABELSCOL2);

                        PlayCmdGrid.Children.Add(opponentScoreValue);
                        Grid.SetRow(opponentScoreValue, CustomDefines.GRID_PLAY_LINE4);
                        Grid.SetColumn(opponentScoreValue, CustomDefines.GRID_PLAY_LABELSCOL4);


                        // 7: Affichage de la grid de jeu après masquage de celle de placement (si affichée)

                        placeBox.Visibility = System.Windows.Visibility.Hidden;
                        playBox.Content = PlayCmdGrid;
                        playBox.Visibility = System.Windows.Visibility.Visible;

                        break;

                    }

                case CustomDefines.DRAW_COMMANDS_NONE:
                    {
                        placeBox.Visibility = System.Windows.Visibility.Hidden;
                        playBox.Visibility = System.Windows.Visibility.Hidden;
                        break;
                    }

            }
        }

        //-------------------------------------------------------------------------------------------------------------------

        public SolidColorBrush GetColorFromType(int _type)
        {
            SolidColorBrush col = Brushes.Green;

            CustomDefines defines = new CustomDefines();

            switch (_type)
            {
                case CustomDefines.SHIP_CARRIER: col = defines.SHIP_CARRIER_COLOR; break;
                case CustomDefines.SHIP_BATTLESHIP: col = defines.SHIP_BATTLESHIP_COLOR; break;
                case CustomDefines.SHIP_CRUISER: col = defines.SHIP_CRUISER_COLOR; break;
                case CustomDefines.SHIP_DESTROYER: col = defines.SHIP_DESTROYER_COLOR; break;
                case CustomDefines.SHIP_SUBMARINE: col = defines.SHIP_SUBMARINE_COLOR; break;
            }

            return col;
        }
        //-------------------------------------------------------------------------------------------------------------------

        public void HideTheRestOfTemporaryShip(Grid TheGrid, int _x, int _y, int _size, int _orient)
        {
            switch (_orient)
            {

                // Y VARIATIONS ///////////////////////////////////////////////////////////////
                case CustomDefines.ORIENT_TOBOTTOM:
                    {
                        for (int yvar = _y + 1; yvar < (_y + _size); yvar++)
                        {
                            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
                            {
                                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;
                                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE) && (tcp.squareProp.X == _x) && (tcp.squareProp.Y == yvar))
                                {
                                    ((Rectangle)fe).Opacity = 1;
                                    ((Rectangle)fe).Fill = Brushes.Transparent;
                                    break;
                                }
                            } // end foreach
                        } // end for y var
                        break;
                    } // end case orientation bottom

                case CustomDefines.ORIENT_TOTOP:
                    {
                        for (int yvar = (_y - _size + 1); yvar < _y; yvar++)
                        {
                            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
                            {
                                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;
                                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE) && (tcp.squareProp.X == _x) && (tcp.squareProp.Y == yvar))
                                {
                                    ((Rectangle)fe).Opacity = 1;
                                    ((Rectangle)fe).Fill = Brushes.Transparent;
                                    break;
                                }
                            } // end foreach
                        } // end for y var
                        break;
                    } // end case orientation top

                // X VARIATIONS ///////////////////////////////////////////////////////////////////////
                case CustomDefines.ORIENT_TOLEFT:
                    {
                        for (int xvar = (_x - _size + 1); xvar < _x; xvar++)
                        {
                            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
                            {
                                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;
                                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE) && (tcp.squareProp.X == xvar) && (tcp.squareProp.Y == _y))
                                {
                                    ((Rectangle)fe).Opacity = 1;
                                    ((Rectangle)fe).Fill = Brushes.Transparent;
                                    break;
                                }
                            } // end foreach
                        } // end for x var
                        break;
                    } // end case orientation toleft

                case CustomDefines.ORIENT_TORIGHT:
                    {
                        for (int xvar = _x + 1; xvar < _x + _size; xvar++)
                        {
                            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
                            {
                                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;
                                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE) && (tcp.squareProp.X == xvar) && (tcp.squareProp.Y == _y))
                                {
                                    ((Rectangle)fe).Opacity = 1;
                                    ((Rectangle)fe).Fill = Brushes.Transparent;
                                    break;
                                }
                            } // end foreach
                        } // end for x var
                        break;
                    } // end case orientation toleft


            } // end switch orientation
        }
        //-------------------------------------------------------------------------------------------------------------------
        public void DrawShip(Grid TheGrid, int _x, int _y, int _type, int _orient, int _size)
        {
            int nextX = _x, nextY = _y;

            for (int i = 0; i < _size; i++)
            {
                SetOccupiedSquare(TheGrid, nextX, nextY, _type);

                if (_orient == CustomDefines.ORIENT_TOBOTTOM)
                {
                    nextY++;
                }
                else if (_orient == CustomDefines.ORIENT_TOTOP)
                {
                    nextY--;
                }
                else if (_orient == CustomDefines.ORIENT_TOLEFT)
                {
                    nextX--;
                }
                else if (_orient == CustomDefines.ORIENT_TORIGHT)
                {
                    nextX++;
                }

            }

            // VERROUILLAGE DES CASES ENVIRONNANTES

            int firstX = (_orient == CustomDefines.ORIENT_TOLEFT ? (_x - _size + 1) : _x);
            int firstY = (_orient == CustomDefines.ORIENT_TOTOP ? (_y - _size + 1) : _y);

            // Verrouillage si horiz
            if (_orient == CustomDefines.ORIENT_TOLEFT || _orient == CustomDefines.ORIENT_TORIGHT)
            {
                for (int currentX = firstX - 1; currentX < firstX + _size + 1; currentX++)
                    for (int currentY = firstY - 1; currentY < firstY + 2; currentY++)
                    {
                        if (Board.IsInBoard(currentX, currentY))
                        {
                            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
                            {
                                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;

                                if (
                                    (tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE)
                                    && (tcp.squareProp.X == currentX) && (tcp.squareProp.Y == currentY)
                                    && (currentY != firstY || (currentX == firstX - 1 || currentX == firstX + _size))  // Si on est bien AUTOUR du bateau et non dedans
                                    )
                                {
                                    tcp.compType = TagComponentProperties.CompTypes.TYPE_UNPLACEABLEWATER;
                                    tcp.identifier = Keys.FORBIDDEN;
                                    break;
                                }


                            }
                        }

                    }

            } // END IF

            else // Verrouillage si vertical
            {
                for (int currentY = firstY - 1; currentY < firstY + _size + 1; currentY++)
                    for (int currentX = firstX - 1; currentX < firstX + 2; currentX++)
                    {
                        if (Board.IsInBoard(currentX, currentY))
                        {
                            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
                            {
                                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;

                                if (
                                    (tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE)
                                    && (tcp.squareProp.X == currentX) && (tcp.squareProp.Y == currentY)
                                    && (currentX != firstX || (currentY == firstY - 1 || currentY == firstY + _size))  // Si on est bien AUTOUR du bateau et non dedans
                                    )
                                {
                                    tcp.compType = TagComponentProperties.CompTypes.TYPE_UNPLACEABLEWATER;
                                    tcp.identifier = Keys.FORBIDDEN;
                                    break;
                                }


                            }
                        }

                    }

            }// END ELSE
        }
        //-------------------------------------------------------------------------------------------------------------------        
        public void EnableShipsButtons(GroupBox pbox)
        {
            SetButtonEnabledStatus(pbox, Keys.BAT1PLBTN, true);
            SetButtonEnabledStatus(pbox, Keys.BAT2PLBTN, true);
            SetButtonEnabledStatus(pbox, Keys.BAT3PLBTN, true);
            SetButtonEnabledStatus(pbox, Keys.BAT4PLBTN, true);
            SetButtonEnabledStatus(pbox, Keys.BAT5PLBTN, true);
        }
        //-------------------------------------------------------------------------------------------------------------------
        public void SetButtonEnabledStatus(GroupBox box, String buttId, bool enabled)
        {

            Grid contGrid = (Grid)box.Content;

            foreach (System.Windows.FrameworkElement fe in contGrid.Children)
            {
                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;

                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_BUTTON) && (tcp.identifier == buttId))
                {
                    ((Button)fe).IsEnabled = enabled;
                    
                    // Cas particuliers des 2 boutons Remove -> adaptation de la couleur de la police
                    if (buttId == Keys.MINRESETBTN || buttId == Keys.RESETSHIPBTN)
                    {
                        if (enabled) ((Button)fe).Foreground = CustomDefines.GRID_PLAC_WARNINGCOLOR;
                        else ((Button)fe).Foreground = Brushes.LightSlateGray;
                    }
                    else if (buttId == Keys.PLAYBTN)
                    {
                        if (enabled) { ((Button)fe).Foreground = CustomDefines.GRID_PLAC_GOCOLOR; ((Button)fe).ToolTip = CustomDefines.TOOLTIP_PLAYBTN_ENABLED; }
                        else { ((Button)fe).Foreground = Brushes.LightSlateGray; ((Button)fe).ToolTip = CustomDefines.TOOLTIP_PLAYBTN_DISABLED; }
                    }
                     
                }
            }
        }
        //-------------------------------------------------------------------------------------------------------------------
        public void UpdateAmountsLabels(GroupBox pbox, int carrLeft, int bshipLeft, int cruiLeft, int destLeft, int submLeft, int mineLeft)
        {
            UpdateLabel(pbox, Keys.BAT1LEFT, carrLeft);
            UpdateLabel(pbox, Keys.BAT2LEFT, bshipLeft);
            UpdateLabel(pbox, Keys.BAT3LEFT, cruiLeft);
            UpdateLabel(pbox, Keys.BAT4LEFT, destLeft);
            UpdateLabel(pbox, Keys.BAT5LEFT, submLeft);
            UpdateLabel(pbox, Keys.MINESLEFT, mineLeft);
        }
        //-------------------------------------------------------------------------------------------------------------------
        public void UpdateLabel(GroupBox box, String buttId, String newContent)
        {
            Grid contGrid = (Grid)box.Content;

            foreach (System.Windows.FrameworkElement fe in contGrid.Children)
            {
                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;

                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_LABEL) && (tcp.identifier == buttId))
                {
                    ((Label)fe).Content = newContent;
                }
            }
        }
        //-------------------------------------------------------------------------------------------------------------------
        public void UpdateLabel(GroupBox box, String buttId, int newContent)
        {
            String str = newContent.ToString();
            UpdateLabel(box, buttId, str);
        }
        //-------------------------------------------------------------------------------------------------------------------
        public void SetOccupiedSquare(Grid TheGrid, int _x, int _y, int _type)
        {
            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
            {
                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;

                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE) && (tcp.squareProp.X == _x) && (tcp.squareProp.Y == _y))
                {
                    ((Rectangle)fe).Opacity = 1;
                    ((Rectangle)fe).Fill = GetColorFromType(_type);
                    tcp.compType = TagComponentProperties.CompTypes.TYPE_SHIP;
                    tcp.identifier = _type.ToString();
                    break;
                }
            }

        }
        //-------------------------------------------------------------------------------------------------------------------
        public void GotoNextOrientationStep(int direction, ref int currShipOrient)
        {
            switch (direction)
            {
                case CustomDefines.WHEEL_HOUR:

                    if (currShipOrient == CustomDefines.ORIENT_TOBOTTOM) currShipOrient = CustomDefines.ORIENT_TOLEFT;
                    else if (currShipOrient == CustomDefines.ORIENT_TOLEFT) currShipOrient = CustomDefines.ORIENT_TOTOP;
                    else if (currShipOrient == CustomDefines.ORIENT_TOTOP) currShipOrient = CustomDefines.ORIENT_TORIGHT;
                    else if (currShipOrient == CustomDefines.ORIENT_TORIGHT) currShipOrient = CustomDefines.ORIENT_TOBOTTOM;

                    break;

                case CustomDefines.WHEEL_ANTIHOUR:

                    if (currShipOrient == CustomDefines.ORIENT_TOBOTTOM) currShipOrient = CustomDefines.ORIENT_TORIGHT;
                    else if (currShipOrient == CustomDefines.ORIENT_TORIGHT) currShipOrient = CustomDefines.ORIENT_TOTOP;
                    else if (currShipOrient == CustomDefines.ORIENT_TOTOP) currShipOrient = CustomDefines.ORIENT_TOLEFT;
                    else if (currShipOrient == CustomDefines.ORIENT_TOLEFT) currShipOrient = CustomDefines.ORIENT_TOBOTTOM;

                    break;
            }
        }
        //-------------------------------------------------------------------------------------------------------------------
        public Boolean TryDrawTemporaryShip(Grid TheGrid, int _xStart, int _yStart, int _size, int _orient)
        {
            PlacedShip temp = new PlacedShip();
            temp.Length = _size;

            if (Board.IsShipPlaceable(TheGrid, temp, _xStart, _yStart, _orient))
            {
                switch (_orient)
                {
                    /// Y VARIATIONS ////////////////////////////////////////
                    case CustomDefines.ORIENT_TOBOTTOM:
                        {
                            for (int i = 0; i < _size; i++)
                            {
                                SetTemporarySquare(TheGrid, _xStart, _yStart + i);
                            }
                            break;
                        }

                    case CustomDefines.ORIENT_TOTOP:
                        {
                            for (int i = 0; i < _size; i++)
                            {
                                SetTemporarySquare(TheGrid, _xStart, _yStart - i);
                            }
                            break;
                        }
                    /// X VARIATIONS //////////////////////////////////////////
                    case CustomDefines.ORIENT_TOLEFT:
                        {
                            for (int i = 0; i < _size; i++)
                            {
                                SetTemporarySquare(TheGrid, _xStart - i, _yStart);
                            }
                            break;
                        }

                    case CustomDefines.ORIENT_TORIGHT:
                        {
                            for (int i = 0; i < _size; i++)
                            {
                                SetTemporarySquare(TheGrid, _xStart + i, _yStart);
                            }
                            break;
                        }

                } // end switch orient  
                return true;

            } // end if is placeable
            else
            {
                return false;
            }

        }
        //-------------------------------------------------------------------------------------------------------------------
        public void SetTemporarySquare(Grid TheGrid, int _x, int _y)
        {
            foreach (System.Windows.FrameworkElement fe in TheGrid.Children)
            {
                TagComponentProperties tcp = (TagComponentProperties)fe.Tag;

                if ((tcp != null) && (tcp.compType == TagComponentProperties.CompTypes.TYPE_SQUARE) && (tcp.squareProp.X == _x) && (tcp.squareProp.Y == _y))
                {
                    ((Rectangle)fe).Opacity = 0.5;
                    ((Rectangle)fe).Fill = Brushes.Red;
                    break;
                }


            }

        }
        //-------------------------------------------------------------------------------------------------------------------
        public static void SetBoatCursor(FrameworkElement parent)
        {
            Uri curi = new Uri("boat1.ani", UriKind.Relative);
            parent.Cursor = new Cursor(App.GetResourceStream(curi).Stream);
        }
        //-------------------------------------------------------------------------------------------------------------------
        public void SetGameContentVisibility(Grid absGrid, Viewbox genVB, Boolean visible)
        {
            if (visible)
            {
                // Let's find the added image

                System.Windows.FrameworkElement img = null;
                System.Windows.FrameworkElement lab = null;

                foreach (System.Windows.FrameworkElement fe in absGrid.Children)
                {
                    if (fe.Name == Keys.GENBGIMAGE)
                    {
                        img = fe;
                    }
                    else if (fe.Name == Keys.GENBGLABEL)
                    {
                        lab = fe;
                    }
                }

                // Let's delete image and label, if found
                if (img != null)
                    absGrid.Children.Remove(img);

                if (lab != null)
                    absGrid.Children.Remove(lab);

                // Show the game
                genVB.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                // Hide the game
                genVB.Visibility = System.Windows.Visibility.Hidden;

                // Let's add the picture
                Image img = new Image();
                img.Name = Keys.GENBGIMAGE;

                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.UriSource = new Uri("pack://application:,,,/img/fond.jpg");
                bmp.EndInit();

                img.Source = bmp;


                Label lab = new Label();
                lab.Content = CustomDefines.WELCOME_MESSAGE;
                lab.Foreground = Brushes.Red;
                lab.FontSize = 40;
                lab.FontWeight = FontWeights.Bold;
                lab.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                lab.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                lab.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                lab.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                lab.Name = Keys.GENBGLABEL;

                Canvas.SetZIndex(img, CustomDefines.BACKGROUND);
                Canvas.SetZIndex(lab, CustomDefines.FOREGROUND);

                absGrid.Children.Add(img);
                Grid.SetRow(img, CustomDefines.ABSGRID_MAINCELL_ROW);
                Grid.SetColumn(img, CustomDefines.ABSGRID_MAINCELL_COLUMN);

                absGrid.Children.Add(lab);
                Grid.SetRow(lab, CustomDefines.ABSGRID_MAINCELL_ROW);
                Grid.SetColumn(lab, CustomDefines.ABSGRID_MAINCELL_COLUMN);

            }

        }


        //-------------------------------------------------------------------------------------------------------------------
        public void RemoveMines(Grid target, Boolean onScreenOnly)
        {
            foreach (System.Windows.FrameworkElement fe3 in target.Children)
            {
                TagComponentProperties tcp3 = (TagComponentProperties)fe3.Tag;
                if (tcp3 != null && (tcp3.compType == TagComponentProperties.CompTypes.TYPE_MINE))
                {
                    ((Rectangle)fe3).Opacity = 1;
                    ((Rectangle)fe3).Fill = Brushes.Transparent;
                    if (!onScreenOnly)
                    {
                        tcp3.compType = TagComponentProperties.CompTypes.TYPE_SQUARE;
                        tcp3.identifier = Keys.SQUARE;
                    }
                }
            }


        }
        //-------------------------------------------------------------------------------------------------------------------
            
    }
}
